package com.example.leetdemo.plan1;

import java.util.*;

public class Sword030 {

    int count = 0;
    List<Integer> list;
    Map<Integer, Integer> map;

    /** Initialize your data structure here. */
    public Sword030() {
        list = new ArrayList<>(8);
        map = new HashMap<>();
    }

    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    public boolean insert(int val) {
        if (map.containsKey(val)) {
            return false;
        }
        list.add(val);
        count++;
        map.put(val, count - 1);
        return true;
    }

    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        if (!map.containsKey(val)) {
            return false;
        }
        int temp = map.get(val);
        list.set(temp, list.get(count - 1));
        count--;
        list.remove(count);
        map.remove(val);
        return true;
    }

    /** Get a random element from the set. */
    public int getRandom() {
        if (list.size() == 0) {
            return 0;
        }
        return list.get((int) ((count) * Math.random()));
    }

    public static void main(String[] args) {
        Sword030 sword030 = new Sword030();
        System.err.println(sword030.insert(10));
        System.err.println(sword030.insert(20));
        System.err.println(sword030.insert(10));
        System.err.println(sword030.getRandom());
        System.err.println(sword030.remove(10));
        System.err.println(sword030.insert(10));
        System.err.println(sword030.getRandom());
    }

}
